package com.classtest.DemoExpend.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.classtest.DemoExpend.bean.DbGoods;
import com.classtest.DemoExpend.bean.DbOrder;
import com.classtest.DemoExpend.bean.DbShop;
import com.classtest.DemoExpend.bean.DbUser;
import com.classtest.DemoExpend.service.DbGoodsService;
import com.classtest.DemoExpend.utils.PKGeneration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.*;

@RestController
public class HomeGoodsController {
    public static Map<String,String> map=new HashMap<>();
    @Autowired
    DbGoodsService dbGoodsService;

    @RequestMapping("/homeGoods")
        // 记录货品数量
    public String homeGoods(HttpServletRequest request, DbGoods dbGoods) {
        DbUser user = (DbUser) request.getSession().getAttribute("user");
        dbGoods.setUsId(user.getId());
        List<DbGoods> userGoodsCount = dbGoodsService.getGoodsCount(JSON.toJSONString(dbGoods));
        int gs_count = 0; // 记录该用户下的货品数量
        Map<String,Object> goodsResult = new HashMap<>();
        for (DbGoods good:userGoodsCount) {
            gs_count += Integer.parseInt(good.getGoodsCount());
        }
        goodsResult.put("gs_count",gs_count);
        return JSON.toJSONString(goodsResult);
    }

    // 取出商品列表
    @RequestMapping("/shopList")
    public String shopList(HttpServletRequest request, DbGoods dbGoods){
        DbUser user = (DbUser) request.getSession().getAttribute("user");
        dbGoods.setUsId(user.getId());
        List<DbGoods> shopList = dbGoodsService.getGoodsCount(JSON.toJSONString(dbGoods));
        Map<String,Object> getShopList = new HashMap<>();
        List<Object> list = new ArrayList<>();
        for (DbGoods goods: shopList){
            Map<String,Object> res = new HashMap<>();
            res.put("id",goods.getId());
            res.put("goodsName",goods.getGoodsName());
            res.put("price",goods.getPrice());
            res.put("size",goods.getSize());
            res.put("color",goods.getColor());
            res.put("goodsCount",goods.getGoodsCount());
            res.put("desc",goods.getDesc());
            list.add(res);
        }
        getShopList.put("goods",list);
        return JSON.toJSONString(getShopList);
    }

    @RequestMapping("/deleteGoods")
    public String deleteGoods(@RequestBody String json) {
        DbGoods dbGoods = JSONObject.parseObject(json, DbGoods.class);
        dbGoodsService.deleteGoods(dbGoods.getId());
        Map<String, Object> deleteGoodsALL = new HashMap<>();
        try {
            deleteGoodsALL.put("flag", "ok");
        }
            catch(Exception e){
            deleteGoodsALL.put("error", e.toString());
        }
        return JSON.toJSONString(deleteGoodsALL);
    }


    @RequestMapping("/insertExImage")
    public String insertExImage(@RequestBody String json, HttpSession session, Model model){
        Map<String,Object> result = new HashMap<>();
        try{
            DbUser user = (DbUser) session.getAttribute("user");
            if(user != null){
                DbGoods dbGoods = JSONObject.parseObject(json,DbGoods.class);
                dbGoods.setUsId(user.getId());
                dbGoods.setId(PKGeneration.INSTANCE.nextId());
                dbGoodsService.insertEXImage(JSON.toJSONString(dbGoods));
//                result.put("gsId",dbGoods.getId());
                map.put("gsId",dbGoods.getId());
                result.put("reFlag","ok");
            }else {
                result.put("reFlag","error");
                result.put("reMsg","还没有登录");
            }
        }catch (Exception e){
            result.put("error",e.toString());
        }
        return JSON.toJSONString(result);
    }

    @RequestMapping("/uploadImage")
    public String uploadImage(@RequestParam("file") MultipartFile file, HttpSession session,Model model){
        Map<String,Object> result = new HashMap<>();
        try{
            Map<String,String> map1 = HomeGoodsController.map;
            String gsId1 = map1.get("gsId");
            if(gsId1 != null){
                DbGoods dbGoods = new DbGoods();
                dbGoods.setId(gsId1);
                dbGoods.setImageR("C:\\Users\\11939\\Pictures\\lassTestImage");
                dbGoodsService.uploadImage(JSON.toJSONString(dbGoods),file);
                session.removeAttribute("gsId");
                result.put("reFlag","ok");
            }else {
                result.put("reFalg","error");
                result.put("reMsg","上传失败");
            }
        }catch (Exception e){
            result.put("error",e.toString());
        }
        return JSON.toJSONString(result);
    }
}
